package model.classesDAO;

import java.util.List;

import javax.persistence.TypedQuery;

import model.classes.Modelo;
import model.interfacesDAO.IModeloDAO;

public class ModeloDAO extends GenericoDAO<Modelo> implements IModeloDAO{

	@Override
	public void cadastrar(Modelo entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
			this.entityManager.persist(entidade);
			this.entityManager.getTransaction().commit();
	}

	@Override
	public void alterar(Modelo entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
			this.entityManager.persist(entidade);
			this.entityManager.getTransaction().commit();
	}

	@Override
	public void remover(Modelo entidade) {
		if (!this.entityManager.getTransaction().isActive());
		this.entityManager.getTransaction().begin();
		this.entityManager.remove(entidade);
		this.entityManager.getTransaction().commit();	
	}

	@Override
	public Modelo pesquisarID(long id) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
		
		Modelo m = this.entityManager.find(Modelo.class, id); 
		return m;
	}

	@Override
	public List<Modelo> pesquisarTodos() {
		if(!this.entityManager.getTransaction().isActive());
		this.entityManager.getTransaction().begin();
		TypedQuery<Modelo> q = this.entityManager.createQuery("SELECT m FROM Modelo m", Modelo.class);
		List<Modelo> modelos = q.getResultList();
		return modelos;
	}

}
